﻿--[[
   @brief    ui logic for update dlg
   @author   rickonzhang
   @version  1.0
   @date     2015-03-07
--]]

update_dialog_view =
{
    pDialog = nil;
    dwRootId = 0;
    init = function()
        ierd_tgp.tpf_ui.Log("update_dialog_view init.");
        update_dialog_view.pDialog = nil;
        update_dialog_view.dwRootId = 0;
    end;
    
    show = function(advice_update)
        if update_dialog_view.pDialog == nil then
            update_dialog_view.create_dialog();
        end;
        
        if update_dialog_view.pDialog ~= nil then
            update_dialog_view.switch_to_status(advice_update);
            update_dialog_view.pDialog:Show(TRUE);
            update_dialog_view.pDialog:Restore();
            update_dialog_view.set_defualt_version_desp();
            update_dialog_view.pDialog:BringToTop();
        end;
    end;
    
    close = function(uilib, dwID)
        if update_dialog_view.pDialog then
            update_dialog_view.pDialog:Show(FALSE);
        end;
    end;
    
    do_update = function(uilib, dwId)
        --升级
        update_dialog_control.do_update_app();

        -- 数据上报
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local   zData = {};

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kVerUpdateBtnClk;
        zTemp.str_action = "Update";
        zTemp.str_type   = "ClkUpdate";
        zTemp.str_json   = base_utility.table_to_json(zData);

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;
    
    go_to_update_web = function(uilib, dwId)
        --查看升级信息
        update_dialog_control.go_to_update_web();

        -- 数据上报
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local   zData = {};

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kVerUpdateBtnClk;
        zTemp.str_action = "Update";
        zTemp.str_type   = "ClkGoUpdateWeb";
        zTemp.str_json   = base_utility.table_to_json(zData);

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;

    go_to_history_web = function(uilib, dwId)
        --查看历史信息
        update_dialog_control.go_to_history_web();

        -- 数据上报
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local   zData = {};

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kVerUpdateBtnClk;
        zTemp.str_action = "Update";
        zTemp.str_type   = "ClkGoHistoryWeb";
        zTemp.str_json   = base_utility.table_to_json(zData);

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;
    
    switch_to_status = function(need_update)
        local main_uilib = ui_utility.uilib();
        local const_str1, str_holder1 = U8toCUIStr("InnerCtn\\Ctn_NeedUpdate");
        local pNeedUpdateCtrl = main_uilib:GetControlPtr(main_uilib:GetControlIDbyPath(const_str1, update_dialog_view.dwRootId));
        local const_str2, str_holder2 = U8toCUIStr("InnerCtn\\Ctn_NoNeedUpdate");
        local pNoNeedUpdateCtrl = main_uilib:GetControlPtr(main_uilib:GetControlIDbyPath(const_str2, 
                                                           update_dialog_view.dwRootId));
        local highlight_color = "RGB:255,162,0";
        if base_utility.is_type_of_oversea_ver() then
            highlight_color = "RGB:25,178,255";
        end

        if need_update == true then
            ierd_tgp.tpf_ui.Log("have update");
            ui_utility.setproperty_by_path(pNeedUpdateCtrl:GetControlID(), "Btn_UpdateUrl", "TextColor", highlight_color);
            ui_utility.setproperty_by_path(pNeedUpdateCtrl:GetControlID(), "Btn_UpdateUrl", "TextColorHover", highlight_color);
            ui_utility.setproperty_by_path(pNeedUpdateCtrl:GetControlID(), "Btn_UpdateUrl", "TextColorPressed", highlight_color);
            pNeedUpdateCtrl:Show(TRUE);
            pNoNeedUpdateCtrl:Show(FALSE);
        else
            ui_utility.setproperty_by_path(pNoNeedUpdateCtrl:GetControlID(), "Btn_UrlHero", "TextColor", highlight_color);
            ui_utility.setproperty_by_path(pNoNeedUpdateCtrl:GetControlID(), "Btn_UrlHero", "TextColorHover", highlight_color);
            ui_utility.setproperty_by_path(pNoNeedUpdateCtrl:GetControlID(), "Btn_UrlHero", "TextColorPressed", highlight_color);
            pNeedUpdateCtrl:Show(FALSE);
            pNoNeedUpdateCtrl:Show(TRUE);
            ierd_tgp.tpf_ui.Log("have no update");
        end;        
    end;

    -- 设置默认版本特性说明
    set_defualt_version_desp = function ()
        local sTemp   = "";
        if (nil ~= update_dialog_control.sNewVer) then
            sTemp = update_dialog_control.sNewVer;
        else
            local sTGPVer = NewStr(260);
            local pGMLib  = base_utility.get_component_adapter("IGame_library",
                                "ierd_tgp.game_library.Get_game_library_adapter();");
            local bDevVer = pGMLib:get_tgp_version(sTGPVer, 260, false);
            sTemp = CUIStrtoU8(U8toCUIStr(sTGPVer));
        end;
        
        local version_tip_1 = LoadStr("WEGAME_UPDATE_VERSION_TIP1");
        local version_tip_2 = LoadStr("WEGAME_UPDATE_VERSION_TIP2");
        local default_version_info = string.format("1.%s<b>2.%s", version_tip_1, version_tip_2);
        update_dialog_view.set_version_desp(default_version_info, sTemp);
    end;

    -- 设置版本特性说明
    set_version_desp = function(sJson, sVer)
        if nil == sJson or "" == sJson then
            return;
        end

        local desc_info = "<f c=\"0x666666\">"..sJson.."</f>";

        local pCtrl = ui_utility.get_ctrl_ptr_by_path(update_dialog_view.dwRootId,
                            "InnerCtn\\Ctn_NeedUpdate\\svDesp\\edtDesp");
        local interface = "Tenio::IRichTextBox";
        local pEdit = tolua.cast(pCtrl, interface);

        if (nil ~= pEdit) then
            local need_tips = string.format(LoadStr("WEGAME_SYS_MENU_VERSION_DETECT_NEW_TIPS"), sVer);
            local sTips = need_tips.."<b>"..desc_info;
            local sText = U8toCUIStr(sTips);
            pEdit:Clear();
            pEdit:PushBackHtmlString(sText);
            local pView = ui_utility.get_ctrl_ptr_by_path(update_dialog_view.dwRootId,
                                 "InnerCtn\\Ctn_NeedUpdate\\svDesp");
            if (nil ~= pView) then
                pView:SetVScrollPosToStart();
            end;
        end;

        local pEdit = ui_utility.get_ctrl_ptr_by_path(update_dialog_view.dwRootId,
                            "InnerCtn\\Ctn_NoNeedUpdate\\svDesp\\edtDesp");
        if (nil ~= pEdit) then
            local no_need_tips = string.format(LoadStr("WEGAME_SYS_MENU_VERSION_NEWEST_TIPS"), sVer);
            local sTips = no_need_tips.."<b>"..desc_info;
            local sText = U8toCUIStr(sTips);
            pEdit:Clear();
            pEdit:PushBackHtmlString(sText);
            local pView = ui_utility.get_ctrl_ptr_by_path(update_dialog_view.dwRootId,
                                 "InnerCtn\\Ctn_NoNeedUpdate\\svDesp");
            if (nil ~= pView) then
                pView:SetVScrollPosToStart();
            end;
        end;
    end;

    create_dialog = function()
        local main_uilib = ui_utility.uilib();
        update_dialog_view.dwRootId = layout_mgr.restore_layout("wegame_sys_menu\\WeGameVersionDialog.tpf/TenioUILayout/UIObject");
        if update_dialog_view.dwRootId ~= 0 then
            local pControl = main_uilib:GetControlPtr(update_dialog_view.dwRootId);
            update_dialog_view.pDialog = tolua.cast(pControl, "Tenio::ITDialog");                
        else
            ierd_tgp.tpf_ui.Log("create update_dialog_view dialog error!");
        end;
    end;

    close = function(uilib, dwControlId)
        layout_mgr.delay_remove_ctrl(update_dialog_view.dwRootId);
        update_dialog_view.init();
    end;
};

update_dialog_view.init();